from mlwrite import db


class BaseModel(db.Model):
    __abstract__ = True

    def to_json(self, have_child=None):
        dicts = self.__dict__
        if "_sa_instance_state" in dicts:
            del dicts["_sa_instance_state"]
        if have_child:
            dicts['have_child'] = True
        return dicts


class JsonResult:
    code = 0
    message = 'success'
    data = None

    def __init__(self, code, message, data):
        self.code = code
        self.message = message
        self.data = data

    def success(self, data=None):
        self.code = 0
        self.message = 'success'
        self.data = data
        return self.__dict__

    def error(self, code, message):
        self.code = code
        self.message = message
        return self.__dict__
